package com.banzhi.shop.activity;

import android.app.FragmentBreadCrumbs;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.GroupApplyBean;
import com.banzhi.shop.data.GroupTypeBean;
import com.banzhi.shop.data.SelectAddressBean;
import com.banzhi.shop.data.SelectAddressData;
import com.banzhi.shop.entity.GoodsDetails.Buy_Data;
import com.banzhi.shop.model.CallBack.ConfirmOrderTypeCallBack;
import com.banzhi.shop.model.CallBack.SelectAddressCallBack;
import com.banzhi.shop.model.GroupApplyCallBack;
import com.banzhi.shop.model.Impl.ConfirmOrderTypeImpl;
import com.banzhi.shop.model.Impl.GroupApplyImpl;
import com.banzhi.shop.model.Impl.SelectAddressImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.nostra13.universalimageloader.utils.L;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.MyApp.context;

//确认订单_要商品
public class ConfirmOrderGoodsActivity extends BaseActivity implements View.OnClickListener, ConfirmOrderTypeCallBack, SelectAddressCallBack, GroupApplyCallBack {

    private TextView mTvAddress;
    private ImageView mIvBack;
    private ImageView mIvGoods;
    private TextView mTvOpenPrice;
    private TextView mTvGroupNum;
    private TextView mTvGroupPrice;
    private TextView mTvTitle;
    private TextView mTvPay;
    private ConfirmOrderTypeImpl mModel;
    private String mGoodId;
    private GroupTypeBean mGroupTypeBean;
    private LinearLayout mLlAddAddess;
    private SelectAddressImpl mSelectAddressModel;
    private List<SelectAddressBean> mAddressBean;
    private List<SelectAddressBean> mSelectAddressBeans = new ArrayList<>();
    private GroupApplyImpl mGroupApplyModel;
    private String mPlace;
    private String mAddressid;
    private GroupApplyBean mGroupApplyBean;
    private TextView mTvGuigeTitle;
    private String mGuigetitle;
    private String mOptionid;
    private String mAddress;
    private String mProvince;
    private String mCity;
    private String mArea;
    private String mAddress1;
    private String tt;
    private String teamid;
    private String mPrice;
    private String flag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order_goods);
        Translucent.setTranslucent(this);
        initView();
        setListener();
        Intent intent = getIntent();
        mGoodId = intent.getStringExtra("mGoodId");
        mGuigetitle = intent.getStringExtra("guigetitle");
        mOptionid = intent.getStringExtra("optionid");
        teamid = intent.getStringExtra("teamid");
        tt = intent.getStringExtra("tt");
        flag = intent.getStringExtra("flag");
        mPrice = intent.getStringExtra("price");
        SharedPreferences.Editor editor = MyApp.loginMessage.edit();
        if ("1".equals(tt)) {
            editor.putString("teamid", teamid);
            editor.putString("tt", "1");
        }
        editor.apply();
        mModel = new ConfirmOrderTypeImpl();
        mModel.getConfirmOrderTypeData(mGoodId, 2 + "", this);
        mSelectAddressModel = new SelectAddressImpl();
        mSelectAddressModel.selectAddressData("1", 2, this);
        mGroupApplyModel = new GroupApplyImpl();
    }

    private void initView() {
        mLlAddAddess = (LinearLayout) findViewById(R.id.ll_add_address);
        mIvGoods = (ImageView) findViewById(R.id.iv_goods);
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mTvAddress = (TextView) findViewById(R.id.tv_address);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvGroupPrice = (TextView) findViewById(R.id.tv_group_price);
        mTvGroupNum = (TextView) findViewById(R.id.tv_group_num);
        mTvOpenPrice = (TextView) findViewById(R.id.tv_open_price);
        mTvPay = (TextView) findViewById(R.id.tv_pay_goods);
        mTvGuigeTitle = (TextView) findViewById(R.id.tv_guigetitle);
    }

    private void setListener() {
        mIvBack.setOnClickListener(this);
        mTvPay.setOnClickListener(this);
        mLlAddAddess.setOnClickListener(this);

    }

    /**
     * 用于传过去的标志  表示的由选择地址进去的选择地址页面  如果是选择地址进去 选择地址后悔finish()   直接进去的啥也不干
     */
    private boolean isresult = true;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                SharedPreferences.Editor editor = MyApp.loginMessage.edit();
                editor.putString("teamid", "");
                editor.putString("tt", "");
                editor.apply();
                break;
            case R.id.ll_add_address:
                // ToastUtils.makeToastShort("点击");
                Intent intent = new Intent(this, ChoiceAddressActivity.class);
                intent.putExtra("isresult", isresult);
                startActivityForResult(intent, 100);
                break;
            case R.id.tv_pay_goods:
                //跳转支付界面
                if ("".equals(mPlace)) {
                    ToastUtils.makeToastLong("请先选择收货地址");
                } else {
                    LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                    mGroupApplyModel.getGroupApplyData(mOptionid, mGoodId, 2 + "", mAddressid, mPlace, this);
                }
                break;
        }

    }

    @Override
    public void onGetConfirmOrderTypeDataSuccess(Object object) {
        mGroupTypeBean = (GroupTypeBean) object;
        showView();
    }

    private void showView() {

        mTvGuigeTitle.setText(mGuigetitle);
        mTvTitle.setText(mGroupTypeBean.getTitle());
        mTvGroupPrice.setText(mGroupTypeBean.getGroupsprice());
        mTvGroupNum.setText(mGroupTypeBean.getGroupnum());

        if ("1".equals(tt)) {
            mTvOpenPrice.setText(mPrice);
        } else {
            mTvOpenPrice.setText(mGroupTypeBean.getOpenprice());
        }
        if (ASUtils.isCanUse(mGroupTypeBean.getThumb_url().get(0))) {
            Picasso.with(context).load(mGroupTypeBean.getThumb_url().get(0)).placeholder(R.mipmap.zhanweitu2).into(mIvGoods);
        } else {
            mIvGoods.setImageResource(R.mipmap.moren);
        }
    }

    @Override
    public void onGetConfirmOrderTypeDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }

    //获取地址
    @Override
    public void onSelectAddressDataSuccess(Object object) {
        mAddressBean = (List<SelectAddressBean>) object;
        //   showView();
        for (int i = 0; i < mAddressBean.size(); i++) {
            if ("1".equals(mAddressBean.get(i).getIsdefault())) {
                mAddress = mAddressBean.get(i).getAddress();
                mProvince = mAddressBean.get(i).getProvince();
                mCity = mAddressBean.get(i).getCity();
                mArea = mAddressBean.get(i).getArea();
                mPlace = mProvince + mCity + mArea + mAddress;
                mTvAddress.setText(mPlace);
                mAddressid = mAddressBean.get(i).getId();
            }
        }

    }

    @Override
    public void onSelectAddressDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }

    //获取订单号
    @Override
    public void onGetGroupApplyDataSuccess(Object object) {
        mGroupApplyBean = (GroupApplyBean) object;
        Intent intent2 = new Intent(this, CheckstandGroupActivity.class);
        intent2.putExtra("orderno", mGroupApplyBean.getOrderno());
        intent2.putExtra("balance", mGroupApplyBean.getBanlance());
        intent2.putExtra("TT", "2");
        if ("1".equals(tt)) {
            intent2.putExtra("price", mPrice);
        } else {
            intent2.putExtra("price", mGroupApplyBean.getPrice());
        }
        //     LogUtils.e("flag",flag);
        if ("3".equals(flag)) {
            intent2.putExtra("flag", "3");
        }
        intent2.putExtra("teamid", mGroupApplyBean.getTeamid());
        startActivity(intent2);
    }

    @Override
    public void onGetGroupApplyDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        //  NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 100:
                if (resultCode == 1) {
                    mAddress1 = data.getStringExtra("address");

                    mTvAddress.setText(mAddress1);
                }
                break;
        }
    }
}
